import sys
import re
import math

from PyQt5.Qt import *  # noqa

from PyQt5.Qsci import QsciScintilla
from PyQt5 import Qsci
from PyQt5.Qsci import QsciLexerPython


class CodeEditor(QsciScintilla):

    def __init__(self, parent=None):
        super().__init__(parent)

        self.setUtf8(True)
        self.font = QFont()
        self.font.setFamily("Microsoft YaHei UI")
        self.font.setPointSize(13)
        self.font.setFixedPitch(True)
        self.setFont(self.font)
 
        self.fontmetrics = QFontMetrics(self.font)
 
        self.setFont(self.font)
        self.setMarginsFont(self.font)
 
        #set line number width
        self.setMarginWidth(0, self.fontmetrics.width(str(self.lines())) + 5)
        self.setMarginLineNumbers(0, True)
 
        #mesure line
        self.setEdgeMode(QsciScintilla.EdgeLine)
        self.setEdgeColumn(9000)
        self.setEdgeColor(QColor("#BBB8B5"))
 
        #brace match
        self.setBraceMatching(QsciScintilla.StrictBraceMatch)
 
        #current line color
        self.setCaretLineVisible(True)
        self.setCaretLineBackgroundColor(QColor("#2D2D2D"))
        self.setCaretForegroundColor(QColor("white"))
 
        #selection color
        
        self.setSelectionBackgroundColor(QColor("#606060"))
        self.setSelectionForegroundColor(QColor("#FFFFFF"))
 
        #table relative
        self.setIndentationsUseTabs(True)
        self.setIndentationWidth(5)
        self.setTabIndents(True)
        self.setAutoIndent(True)
        self.setBackspaceUnindents(True)
        self.setTabWidth(5)
 
        #indentation guides
        self.setIndentationGuides(True)
 
        #line number margin color
        self.setMarginsBackgroundColor(QColor("#272727"))
        self.setMarginsForegroundColor(QColor("#CCCCCC"))
 
        #folding margin
        self.setFolding(QsciScintilla.PlainFoldStyle)
        self.setMarginWidth(2,12)
        #marker
        self.markerDefine(QsciScintilla.Minus,QsciScintilla.SC_MARKNUM_FOLDEROPEN)
        self.markerDefine(QsciScintilla.Plus,QsciScintilla.SC_MARKNUM_FOLDER)
        self.markerDefine(QsciScintilla.Minus,QsciScintilla.SC_MARKNUM_FOLDEROPENMID)
        self.markerDefine(QsciScintilla.Plus,QsciScintilla.SC_MARKNUM_FOLDEREND)
        
        #marker define color
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEREND)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEREND)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEROPENMID)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEROPENMID)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERMIDTAIL)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERMIDTAIL)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERTAIL)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERTAIL)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERSUB)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERSUB)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDER)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDER)
        self.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEROPEN)
        self.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEROPEN)
        self.setFoldMarginColors(QColor("#272727"),QColor("#272727"))
 
        #whitespace
        self.setWhitespaceVisibility(QsciScintilla.WsInvisible)
        self.setWhitespaceSize(2)
        """
        the default margin is:
        0: line number,width is not zero
        1: width is zero
        2: folding, width is not zero
        """
        self.setMarginWidth(1,0)
 
        #set lexer
        self.lexer = QsciLexerPython()
        self.lexer.setFont(self.font)
        self.lexer.setColor(QColor("#ffffff"))
        self.setLexer(self.lexer)
 
        #high light code
        self.lexer.setColor(QColor("#ffffff"))
        self.lexer.setPaper(QColor("#333333"))
        self.lexer.setColor(QColor("#5BA5F7"),QsciLexerPython.ClassName)
        self.lexer.setColor(QColor("#FF0B66"),QsciLexerPython.Keyword)
        self.lexer.setColor(QColor("#00FF40"),QsciLexerPython.Comment)
        self.lexer.setColor(QColor("#BD4FE8"),QsciLexerPython.Number)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.DoubleQuotedString)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.TripleSingleQuotedString)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.TripleDoubleQuotedString)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.DoubleQuotedString)
        self.lexer.setColor(QColor("#04F452"),QsciLexerPython.FunctionMethodName)
        self.lexer.setColor(QColor("#FFFFFF"),QsciLexerPython.Operator)
        self.lexer.setColor(QColor("#FFFFFF"),QsciLexerPython.Identifier)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.CommentBlock)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.UnclosedString)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.HighlightedIdentifier)
        self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.Decorator)
 
       
        self.setMarginWidth(0, self.fontmetrics.width(str(self.lines())) + 5)
        self.linesChanged.connect(self.linesChange)


    def linesChange(self):
        # print(self.lines())
        self.setMarginWidth(0, self.fontmetrics.width(str(self.lines())) + 5)


